package com.wxw.BaseLibrary.component;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.GradientDrawable;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.wxw.BaseLibrary.drawable.PanelStyle;
import com.wxw.BaseLibrary.element.IListBoxItem;

/**
 * Created by david on 2014-07-11.
 */
public class ListBoxItem implements IListBoxItem {
    private TextView tv_caption, tv_value;
    private ListBox parent;
    private String key;
    private boolean bChanged = false;
    private int[] BackgroundColor = new int[]{Color.parseColor("#ffffff")};
    private int BorderColor = Color.parseColor("#17a0ff");
    private int BorderWidth = 0;
    private int radians = 0;
    private LinearLayout linearLayout;
    private ListBox.OnListBoxItemChangedListener listBoxItemChangedListener;

    public ListBoxItem(ListBox parent) {
        this.parent = parent;
    }

    @Override
    public View getView() {
        if (linearLayout==null) {
            initView();
        }
        return linearLayout;
    }

    public void setCaption(String caption){
        if (tv_caption != null) {
            tv_caption.setText(caption);
        }
    }

    public String getCaption() {
        return tv_caption != null?tv_caption.getText().toString():"";
    }

    public void setValue(String value) {
        if (tv_value != null) {
            tv_value.setText(value);
        }
    }

    public String getValue() {
        return tv_value != null?tv_value.getText().toString():"";
    }

    @Override
    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public String getKey() {
        return key;
    }

    @Override
    public void setChanged(boolean b) {
        bChanged = b;
        tv_value.setBackground(PanelStyle.createPanelStyle(GradientDrawable.Orientation.TOP_BOTTOM,
                new int[]{(bChanged?parent.getItemChangedBgColor():Color.WHITE)}, 0, 0, 0));

    }

    @Override
    public boolean isChanged() {
        return bChanged;
    }

    @Override
    public void setOnListBoxItemChangedListener(ListBox.OnListBoxItemChangedListener listener) {
        listBoxItemChangedListener = listener;
    }

    @Override
    public ListBox getParent() {
        return parent;
    }

    public int[] getBackgroundColor() {
        return BackgroundColor;
    }

    public void setBackgroundColor(int[] backgroundColor) {
        BackgroundColor = backgroundColor;
    }

    public int getBorderColor() {
        return BorderColor;
    }

    public void setBorderColor(int borderColor) {
        BorderColor = borderColor;
    }

    public int getBorderWidth() {
        return BorderWidth;
    }

    public void setBorderWidth(int borderWidth) {
        BorderWidth = borderWidth;
    }

    public int getRadians() {
        return radians;
    }

    public void setRadians(int radians) {
        this.radians = radians;
    }

    private void initView(){
        linearLayout = new LinearLayout(parent.getContext());
        linearLayout.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout.setBackground(PanelStyle.createPanelStyle(null, BackgroundColor, BorderWidth, BorderColor, radians));

        tv_caption = new TextView(parent.getContext());
        tv_caption.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
        tv_caption.setBackgroundColor(Color.WHITE);
        tv_caption.setTextColor(Color.BLACK);
        linearLayout.addView(tv_caption, new LinearLayout.LayoutParams(this.parent.getCaptionWidth(),
                LinearLayout.LayoutParams.MATCH_PARENT));

        tv_value = new TextView(parent.getContext());
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
        layoutParams.setMargins(1, 1, 1, 1);
//        layoutParams.weight = 1;
        tv_value.setLayoutParams(layoutParams);
        tv_value.setBackgroundColor(Color.TRANSPARENT);
        tv_value.setTextColor(Color.BLACK);
        tv_value.setTextSize(TypedValue.COMPLEX_UNIT_SP, this.parent.getItemFontSize());
        linearLayout.addView(tv_value);
    }
}
